package function;

public class CreateQuery 
{
	TableBean[] tb;
	SequenceBean sb;
	String nm;
	String[] stringType= {"CHAR","NCHAR","VARCHAR2","NVARCHAR2","LONG","RAW","LONG RAW","NUMBER","DATE","BLOB","CLOB","NCLOB","BFILE"};
	public CreateQuery(TableBean[] tb, String nm)
	{
		this.tb = tb;
		this.nm = nm;
	}
	public CreateQuery(SequenceBean sb)
	{
		this.sb = sb;
	}
	
	public String createSequence()
	{
		String query = "CREATE SEQUENCE "+sb.getName()+ "\n";
		query+="increment by "+ sb.getIncrement()+"\n";
		query+="start with "+ sb.getStart()+"\n";
		
		if(!sb.isNoMax())
			query+="maxvalue "+sb.getMax()+"\n";
		else
			query+="nomaxvalue\n";
		query+="minvalue "+sb.getMin()+"\n";
		
		if(sb.isCycle())
			query+="cycle\n";
		else
			query+="nocycle\n";
		
		if(sb.isNoCache())
			query+="nocache";
		else
			query += "cache "+ sb.getCache();
		System.out.println(query);
		return query;
	}
	
	public String returnQuery()
	{
		String query="Create table "+nm+"\n";
		query+="(\n";
		String primary="";
		for(int i=0; i<tb.length;i++)
		{
			TableBean tb1 = tb[i];
			query+=tb1.getName()+" "+tb1.getType()+"("+tb1.getSize()+" "+tb1.getByteOrChar()+") ";
			
			if(tb1.isPrimary())
			{
				primary=",CONSTRAINT "+nm+"_PK PRIMARY KEY("+tb1.getName()+")";
			}
			
			if(!tb1.getBasicData().trim().equals(""))
			{
				query +="default ";
				if(tb1.getType()=="LONG"||tb1.getType()=="RAW"||tb1.getType()=="LONG RAW"||tb1.getType()=="NUMBER")
					query+=tb1.getBasicData()+" ";
				else if(tb1.getType()=="DATE")
					query +="TO_DATE('"+tb1.getBasicData()+"') ";
				else
					query +="'"+tb1.getBasicData()+"' ";
			}
			
			if(tb1.isNull())
				query+= "Not null ";
			if(i<tb.length-1)
				query+=",\n";
			else
				query+="\n";
		}
		
		query+=primary+"\n)";
		return query;
	}
}
